<template>
	<view>
		<uni-card>
			<view>
				<view>
					<view class="img"><image :src="addr"></image></view>
				    <view class="name">{{name}}</view>
				</view>
			    <view>本月支出：{{outSum}}</view>
				<view>本月收入：{{inSum}}</view>
				<view>总计：{{sum}}</view>
			</view>
			
		</uni-card>
        <view><button @click="wxLogin()" :disabled="login">登录</button></view>
	</view>
	
</template>

<script>
	import util from "../../util/util.js"
	export default{
		
		data(){
			let {sum,outSum,inSum}=util.sum(getApp().globalData.listData)
			return{
				imgUrl:"",
				sum:sum,
				outSum:outSum,
				inSum:inSum,
				login:getApp().globalData.login
			}
		},
		computed:{
			addr(){
				return getApp().globalData.login?getApp().globalData.userData.avatarUrl:"../../static/tabBar/mine.png"
			},
			name(){
				return getApp().globalData.login?getApp().globalData.userData.nickName:"请登录"
			},
		},
		async onShow(){
			if(getApp().globalData.loginUpdate){
				let res=await uniCloud.callFunction({
					name:"getDatas",
					data:{openid:getApp().globalData.userData.openid}
				})
				console.log(res)
				getApp().globalData.loginUpdate=false
				getApp().globalData.listData=res.result
			}
			console.log(getApp().globalData.listData)
			const {sum,inSum,outSum}=util.sum(getApp().globalData.listData)
			this.sum=sum
			this.inSum=inSum
			this.outSum=outSum
		},
		methods:{
			wxLogin() {
				let _this = this;
				let code;
				uni.login({
					provider: 'weixin',
					success: res => {
						code = res.code;//获取登录需要的code
						// this.name=res.nikname
						// this.imgUrl=res.avatarUrl
					}
				});
				uni.getUserProfile({
					desc: '用户信息，拿来吧你！',
					lang: 'zh_CN',
					success: res => {
						uni.showLoading();
						let data = res.userInfo;//授权拿到用户信息
						data.code = code;
						// 调用登录云函数
						// console.log(data)
						// console.log(_this.$cloudApi)
						_this.$cloudApi.call({
							name: 'login',
							data,
							success: res => {
								console.log(res.userInfo)
								uni.setStorageSync('userInfo', res.userInfo);//拿到后台返回的用户信息
								const {openid,nickName,avatarUrl}=res.userInfo
								getApp().globalData.userData={openid,nickName,avatarUrl}
								getApp().globalData.login=true
								getApp().globalData.loginUpdate=true
								console.log(getApp().globalData)
								console.log(getApp().globalData.userData)
								uni.reLaunch({
									     url:'/pages/mine/mine'
								})
								
							}
						})
						;
					}
				});
			},

		}
	}
</script>

<style lang="scss">
	uni-card{
		.img{
			display: inline-block;
			image{
				height: 100rpx;
				width: 100rpx;
			}
			width: 100rpx;
			height: 100rpx;
		}
		.name{
			display: inline-block;
			width: 100rpx;
		}
	}
</style>